import java.util.*;
import java.sql.*;

public class IndustryFileToTable {

    private String fileName    = null;
    private String tableName   = null;
    private String sectorId    = null;

    public static void main (String args[]) {
	String file_name  = args[0];
	String table_name = "Industry";
	String sector_id  = args[1];

	String query_string = null;
	String data_dir = "./Data/Sector/";

	IndustryFileToTable ft = new IndustryFileToTable ();

	ft.sectorId = sector_id;
	ft.setFileName (data_dir + file_name);
	ft.setTableName (table_name);
	ft.loadIndustryFileToTable();
    }

    public void loadIndustryFileToTable () {

	Hashtable s_hashtable = new Hashtable ();
	Hashtable i_hashtable = new Hashtable ();
	String query_string = null;
	String s_id = null;
	String s_value = null;
	int int_s_id = 0;
	String i_id = null;
	String i_value = null;
	int int_i_id = 0;
	SectorFileLoader fl = new SectorFileLoader ();

	fl.setFileName("./Data/Sector/Sector.txt");
	fl.loadFile();
	s_hashtable = fl.getHashTable();

	SectorFileLoader fl2 = new SectorFileLoader ();
	fl2.setFileName(fileName);
	fl2.loadFile();
	i_hashtable = fl2.getHashTable();

	DataBaseInsert dbInsert = new DataBaseInsert();

	Enumeration enum = i_hashtable.keys();  
	while (enum.hasMoreElements()) {
	    i_id     = (String)enum.nextElement();
	    int_i_id = Integer.parseInt(i_id);
	    i_value  = (String)i_hashtable.get(i_id);

	    s_id     = sectorId;
	    int_s_id = Integer.parseInt(s_id);
	    s_value  = getHashtableValueByKey(s_hashtable, s_id);

	    query_string = "INSERT INTO " + tableName +" VALUES ("+
		"'"+i_value.replaceAll(" ","") +"'," + int_i_id + ","+
		"'"+s_value +"'," + int_s_id +
		");";
	    System.out.println(query_string);
	    dbInsert.setQueryString(query_string);
	    dbInsert.insertData();
	}
    }

    public String getHashtableValueByKey(Hashtable ht, String key )
    {
	String temp = null;
	String value = null;

	Enumeration enum = ht.keys();  
	while (enum.hasMoreElements()) {
	    temp  = (String)enum.nextElement();
	    if (temp.equals(key)) {
		value  = (String)ht.get(temp);
	    }
	}

	return value;
    }
    
    public void setTableName( String table_name )
    {
	tableName = table_name;
    }

    public void setFileName( String file_name )
    {
	fileName = file_name;
    }
}

